import pygame
from pygame.sprite import Sprite


class Alien(Sprite):
    def __init__(self, screen, settings):
        super().__init__()
        self.screen = screen
        self.settings = settings

        # 加载外星人图像，获取其外接矩形
        self.image = pygame.image.load('images/alien.bmp')
        self.rect = self.image.get_rect()
        self.screen_rect = screen.get_rect()

        # 起始出现在屏幕左上角
        self.rect.x = self.rect.width
        self.rect.y = self.rect.height
        self.center = float(self.rect.centerx)

        # 位置标志
        self.x = float(self.rect.x)

    def check_edge(self):
        """检测外星人是否碰到边"""
        if self.rect.right >= self.screen.get_rect().right:
            return True
        if self.rect.left <= 0:
            return True

    def update(self):
        """向左或右移动外星人"""
        self.x += self.settings.alien_speed_factor * self.settings.alien_direct
        self.rect.x = self.x

    def bliteme(self):
        self.screen.blit(self.image, self.rect)
